package com.luna.dao;

import com.luna.beans.Phase;
import com.luna.utils.JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PhaseDao {
    public Phase selectByPhaseId(int phase_id) throws Exception {
        Phase phase = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        Connection con = null;
        String sql = "select * from phase where id=?";
        try {
            con = JDBC.getConnection();
            ps = con.prepareStatement(sql);
            ps.setInt(1, phase_id);
            rs = ps.executeQuery();
            if (rs.next()) {
                phase = new Phase(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("content"),
                        rs.getInt("book_id")
                );
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBC.close(rs, ps, con);
        }
        return phase;
    }

    public List<Phase> selectAllPhase() throws Exception {
        List<Phase> list = new ArrayList<Phase>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from phase";
        try {
            con = JDBC.getConnection();
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Phase(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("content"),
                        rs.getInt("book_id")
                ));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBC.close(rs, ps, con);
        }
        return list;
    }

    public List<Phase> selectAllPhaseByBookId(int book_id) throws Exception {
        List<Phase> list = new ArrayList<Phase>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from phase where book_id=?";
        try {
            con = JDBC.getConnection();
            ps = con.prepareStatement(sql);
            ps.setInt(1, book_id);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Phase(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("content"),
                        rs.getInt("book_id")
                ));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBC.close(rs, ps, con);
        }
        return list;
    }

    public void insertPhase(Phase phase) throws Exception {
        Connection con = null;
        PreparedStatement ps = null;
        String sql = "insert into phase(id, name, content, book_id) value(NULL, ?, ?, ?)";
        try {
            con = JDBC.getConnection();
            ps = con.prepareStatement(sql);
            ps.setString(1, phase.getName());
            ps.setString(2, phase.getContent());
            ps.setInt(3, phase.getBook_id());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBC.close(null, ps, con);
        }
    }

    public void dropPhaseById(int phase_id) throws Exception {
        Connection con = null;
        PreparedStatement ps = null;
        String sql = "delete from phase where id=?";
        try {
            con = JDBC.getConnection();
            ps = con.prepareStatement(sql);
            ps.setInt(1, phase_id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBC.close(null, ps, con);
        }
    }

    public void updatePhase(int id, Phase phase) throws Exception {
        Connection con = null;
        PreparedStatement ps = null;
        String sql = "update phase set name=?, content=? where id=?";
        try {
            con = JDBC.getConnection();
            ps.setString(1, phase.getName());
            ps.setString(2, phase.getContent());
            ps.setInt(3, id);
            ps = con.prepareStatement(sql);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JDBC.close(null, ps, con);
        }
    }
}
